// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/linkage/api/LinkageAdminApi'

import ComClass from '@api/ComQuery'

import ListClass from '@api/linkage/model/RuleQueryReq'
import OneClass from '@api/linkage/model/RuleDetailReq'
import AddClass from '@api/linkage/model/RuleCreateReq'
import EditClass from '@api/linkage/model/RuleEditReq'
import DelClass from '@api/linkage/model/RuleDelReq'

import BaseClass from '@api/linkage/model/LinkageRuleDto'
import ChildClass from '@api/linkage/model/LinkageTargetDto'

const CLIENT = CORE.grpcCLIENT(Client)

const COM = [ComClass, 'comQuery']
const DEVICE = [ChildClass, 'deviceMatchDevice', 1]
const USER = [ChildClass, 'actionSmsUser', 1]
const BASE = [BaseClass, 'content', null, (dto, obj) => CORE.grpcDTO(dto, obj, DEVICE, USER)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'ruleQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne(one, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { ruleId: one.id })
    const resp = await CORE.grpc(TIP, CLIENT, 'ruleDetail', DTO)
    return resp.content || one
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'ruleCreate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'ruleEdit', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ruleId: id })
    await CORE.grpc(TIP, CLIENT, 'ruleDel', DTO)
    return CORE.grpcOK(TIP)
  },
}
